﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using API.Models;
using API.Models.Repository;
using System.Threading.Tasks;

namespace API.Controllers.api.v2
{
    [API.Core.ActionFilters.CustomQueryable]
    [API.Core.ActionFilters.Throttle(Name = "BasicController", Milliseconds = 500)]
    [API.Core.AuthorizeAttribute.EdexAuthorizeAttribute]
    public class BasicApiController : ApiController
    {
        public SEC_SESSIONS currentSession
        {
            get
            {
                return (SEC_SESSIONS)this.ControllerContext.RouteData.Values["session"];
            }
        }

        public BasicApiController()
        { 
            var sessionRepository = new SEC_SESSIONS_Repository();
        }
    }

    public class CommonApiControllerAsync<T> : BasicApiController where T : class, ICommonDbClass
    {
        /// <summary>
        /// Получение списка сущности
        /// </summary>
        /// <returns></returns>
        //[API.Core.ActionFilters.Throttle(Name = "CommonControllerAsyncGet", Milliseconds = 1000)]
        [API.Core.ActionFilters.EFExpandActionFilter]
        public virtual async Task<IQueryable<T>> Get()
        {
            var repository = RepositoryFactoryAsync<T>.Create();
            repository.SetSession(this.currentSession);

            var data = await repository.Find();
            return data;
        }

        [API.Core.ActionFilters.EFExpandActionFilter]
        public virtual async Task<T> Get(Guid id)
        {
            var repository = RepositoryFactoryAsync<T>.Create();
            repository.SetSession(this.currentSession);

            var data = await repository.Find(r => r.GUID_RECORD == id);
            return data.SingleOrDefault();
        }

        public virtual async void Post(T value)
        {
            if (value.GUID_RECORD != null && 
                value.GUID_RECORD != Guid.Empty)
            {
                this.Put(value);
                return;
            }

            var repository = RepositoryFactoryAsync<T>.Create();
            repository.SetSession(this.currentSession);

            value.GUID_RECORD = Guid.NewGuid();

            await repository.Add(value);
            await repository.Save();
        }

        public virtual async void Put(T value)
        {
            var repository = RepositoryFactoryAsync<T>.Create();
            repository.SetSession(this.currentSession);

            await repository.Edit(value);
            await repository.Save();
        }

        public virtual async void Delete(Guid id)
        {
            var repository = RepositoryFactoryAsync<T>.Create();
            repository.SetSession(this.currentSession);

            var data = await repository.Find(r => r.GUID_RECORD == id);
            var entity = data.FirstOrDefault();
            await repository.Delete(entity);
            await repository.Save();
        }
    }

}
